import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/widgets/button/tap_container.dart";
import "package:ct_pa_majordomo/widgets/divider/solid_divider.dart";
import "package:flutter/material.dart";

class MenuBlockItem {
  final bool showArrow;
  final String text;
  final void Function()? onTap;
  final Widget? prefix;
  final Widget? suffix;
  final Widget? tip;
  final Widget? content;

  MenuBlockItem({
    this.showArrow = true,
    required this.text,
    this.onTap,
    this.prefix,
    this.suffix,
    this.tip,
    this.content,
  });
}

class MenuBlock extends StatelessWidget {
  final BorderRadiusGeometry? borderRadius;
  final List<MenuBlockItem> items;
  final Widget? title;
  final Widget? content;

  const MenuBlock({super.key, this.borderRadius, required this.items, this.title, this.content});

  @override
  Widget build(BuildContext context) {
    // if (items.isEmpty) return const SizedBox();
    ThemeData themeData = Theme.of(context);
    return Theme(
      data: themeData.copyWith(
        inputDecorationTheme: themeData.inputDecorationTheme.copyWith(
          border: InputBorder.none,
          contentPadding: EdgeInsets.zero,
          enabledBorder: InputBorder.none,
        ),
      ),
      child: DefaultTextStyle(
        style: DefaultTextStyle.of(context).style.copyWith(fontSize: 16),
        child: Container(
          clipBehavior: Clip.antiAlias,
          decoration: BoxDecoration(
            borderRadius: borderRadius ?? BorderRadius.circular(6),
            color: Colors.white,
          ),
          child: Padding(
            padding: const EdgeInsets.only(right: 10, left: 15, top: 0, bottom: 12),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                if (title != null) Padding(
                  padding: const EdgeInsets.only(top: 12, bottom: 0),
                  child: title,
                ),
                if (content != null) Padding(
                  padding: const EdgeInsets.only(top: 12, bottom: 0),
                  child: content,
                ),
                if (items.isNotEmpty) ...List.generate(items.length * 2 - 1, (index) {
                  if (index.isOdd) {
                    return const SolidDivider(padding: EdgeInsets.only(left: 15));
                  }
                  MenuBlockItem ele = items[index ~/ 2];
                  bool lastIndex = items.length * 2 - 2 == index;
                  return TapContainer(
                      padding: EdgeInsets.only(top: 12, bottom: lastIndex ? 0 : 12),
                      // height: 56,
                      onTap: ele.onTap,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          ele.content ?? Row(
                            children: [
                              if (ele.prefix != null) ...[
                                ele.prefix!,
                                const SizedBox(width: 10),
                              ],
                              Text(ele.text),
                              const Expanded(child: SizedBox()),
                              if (ele.suffix != null)
                                ConstrainedBox(
                                  constraints: const BoxConstraints(maxWidth: 200),
                                  child: ele.suffix,
                                ),
                              if (ele.showArrow)
                                const Icon(
                                  Icons.chevron_right_rounded,
                                  color: ThemeVars.colorBorder,
                                  size: 24,
                                ),
                            ],
                          ),
                          if (ele.tip != null) ...[const SizedBox(height: 6,), ele.tip ?? const SizedBox()],
                        ],
                      )
                  );
                })
              ],
            ),
          )
        ),
      ),
    );
  }
}
